﻿using System;
using System.Collections.Generic;

namespace FallingRox
{
    public abstract class GameObject : ICollidable, IRenderable
    {
        // The position variable will hold the coordinates of the most top left element of the object
        public MatrixCoords Position { get; set; }

        public char[,] Body { get; set; }

        // This will show if the current object should be printed on the console e.i. if it exists on the playfield or if it is gone
        public bool IsDestroyed { get; set; }

        // ICollidable Methods
        public List<MatrixCoords> GetImageCoordinatesMatrix()
        {
            MatrixCoords element;
            List<MatrixCoords> profile = new List<MatrixCoords>(2);
            for (int i = 0; i < this.Body.GetLength(0); i++)
            {
                for (int j = 0; j < this.Body.GetLength(1); j++)
                {
                    if (Body[i, j] != ' ')
                    {
                        profile.Add(element = new MatrixCoords(this.Position.Row + i, this.Position.Col + j));
                    }
                }
            }
            return profile;
        }

        protected char[,] CopyMatrix(char[,] matrix)
        {
            int rows = matrix.GetLength(0);
            int cols = matrix.GetLength(1);

            char[,] result = new char[rows, cols];

            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols; col++)
                {
                    result[row, col] = matrix[row, col];
                }
            }
            return result;
        }

        public string GetObjectType()
        {
            return "GameObject";
        }

        public bool CanCollideWith(string objectType)
        {
            if (objectType == "GameObject")
            {
                return true;
            }
            return false;
        }

        public virtual void RespondToCollision()
        {
        }

        public virtual char[,] GetImage()
        {
            return this.CopyMatrix(this.Body);
        }

        public virtual MatrixCoords GetPosition()
        {
            return this.Position;
        }
    }
}
